using System;
using System.Runtime.InteropServices;
using System.Resources;
using System.IO;

namespace PacSnake
{
	/// <summary>
	/// This is a class that we use to play sounds. I got the code from:
	/// http://www.eggheadcafe.com/articles/20030302.asp
	/// </summary>
	public class WinMM
	{
        public const UInt32 SND_ASYNC = 1;
        public const UInt32 SND_MEMORY = 4;

        // This is the overload we want to play embedded resource...
        [DllImport("Winmm.dll")]
        public static extern bool PlaySound(
            byte[] data, IntPtr hMod, UInt32 dwFlags);


        /// <summary>
        /// Plays an embedded WAV file
        /// </summary>
        /// <param name="wav">filename of the WAV file</param>
        public static void PlayWavResource(string wav)
        {
            // Get the namespace
            string strNameSpace = System.Reflection.Assembly.
                GetExecutingAssembly().GetName().Name.ToString();

            // Get the resource into a stream
            Stream str = System.Reflection.Assembly.GetExecutingAssembly().
                GetManifestResourceStream(strNameSpace + "." + wav);
            
            if (str == null)
                return;
            
            // Bring stream into a byte array
            byte[] bStr = new Byte[str.Length];
            str.Read(bStr, 0, (int)str.Length);
            
            // Play the resource
            PlaySound(bStr, IntPtr.Zero, SND_ASYNC | SND_MEMORY);
        }
	}
}
